home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 051-075 / disk_065 / prep / ifdef.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  5KB  |  217 lines

  1. /* Routines related to conditional compilation.  Ignore_flag is
  2.  * a global external that controls input.  If ignore_flag is TRUE
  3.  * input is ignored.  File inclusion stuff is also here.
  4.  */
  5.  
  6. #include "prep.h"
  7.  
  8. int    ifdef_list[NESTING], ifdef_count ;
  9.  
  10.  
  11.  
  12.  
  13. /* Function IFDEF_PROC
  14.  *
  15.  * #ifdef name1 name2 name3....namen
  16.  * 
  17.  * Different from the cpp conditional compilation directive, since
  18.  * in PREP the symbols | and & (and nearly anything) are legal macro
  19.  * names.  Here the instructions in the #if block will be kept if
  20.  * ANY of the names are defined.  The names must be separated by
  21.  * blanks or tabs.
  22.  */
  23.  
  24. ifdef_proc()     
  25. {                  
  26. int    i ;
  27. char    *name, *pntr ;
  28.  
  29. /* keep track of the nesting */
  30. ifdef_count++ ;
  31. if ( ifdef_count >= NESTING ) {
  32.     sprintf( errline, "#Ifdef: nesting too deep: %s", in_buff ) ;
  33.     abort( errline ) ;
  34. }
  35.  
  36. /* see if any of the tokens is a macro name */
  37. i = ifdef_count - 1 ;
  38. ifdef_list[i] = FALSE ;
  39. for (pntr = first_nonblank + name_length;; pntr = NULL ) {
  40.     if ( NULL == ( name = strtok( pntr, " \t" ) ) ) break ;
  41.     if ( mac_query(name) >= 0 ) {
  42.         ifdef_list[i] = TRUE ;
  43.         break ;
  44.     }
  45. }
  46.  
  47. /* set a flag to inhibit input if any ifdef flags are FALSE */
  48. ignore_flag = FALSE ;
  49. for ( i=0; i<ifdef_count; i++ )
  50.     if ( ifdef_list[i] == FALSE ) ignore_flag = TRUE ;
  51.  
  52. /* signal that in_buff is empty */
  53. IN_BUFF_DONE
  54. }
  55.  
  56.  
  57.  
  58. /* Function IFNDEF_PROC
  59.  *
  60.  * #ifndef name1 name2 name3....namen
  61.  * 
  62.  * Here the instructions in the #ifndef block will be kept if
  63.  * ANY of the names are NOT defined.  The names must be separated by
  64.  * blanks or tabs.
  65.  */
  66.  
  67. ifndef_proc()     
  68. {                  
  69. int    i ;
  70. char    *name, *pntr ;
  71.  
  72. /* keep track of the nesting */
  73. ifdef_count++ ;
  74. if ( ifdef_count >= NESTING ) {
  75.     sprintf( errline, "#Ifdef: nesting too deep: %s", in_buff ) ;
  76.     abort( errline ) ;
  77. }
  78.  
  79. /* see if any of the tokens is not a macro name */
  80. i = ifdef_count - 1 ;
  81. ifdef_list[i] = FALSE ;
  82. for (pntr = first_nonblank + name_length;; pntr = NULL ) {
  83.     if ( NULL == ( name = strtok( pntr, " \t" ) ) ) break ;
  84.     if ( mac_query(name) < 0 ) {
  85.         ifdef_list[i] = TRUE ;
  86.         break ;
  87.     }
  88. }
  89.  
  90. /* set a flag to inhibit input if any ifdef flags are FALSE */
  91. ignore_flag = FALSE ;
  92. for ( i=0; i<ifdef_count; i++ )
  93.     if ( ifdef_list[i] == FALSE ) ignore_flag = TRUE ;
  94.  
  95. /* signal that in_buff is empty */
  96. IN_BUFF_DONE
  97. }
  98.  
  99.  
  100.  
  101. /* ELSE_PROC
  102.  *
  103.  * #else conditional compilation directive.
  104.  */
  105. else_proc()
  106. {
  107. int    i ;
  108.  
  109. /* on missing #ifdef statement, abort */
  110. if ( ifdef_count <= 0 ) {
  111.     sprintf( errline, "#Else: no matching ifdef: %s", in_buff ) ;
  112.     abort( errline ) ;
  113. }
  114.  
  115. ifdef_list[ ifdef_count-1 ] = NOT ifdef_list[ ifdef_count-1 ] ;
  116.  
  117. /* set a flag to inhibit input if any ifdef flags are FALSE */
  118. ignore_flag = FALSE ;
  119. for ( i=0; i<ifdef_count; i++ )
  120.     if ( ifdef_list[i] == FALSE ) ignore_flag = TRUE ;
  121.  
  122. /* signal that in_buff is empty */
  123. IN_BUFF_DONE
  124. }
  125.  
  126.  
  127.  
  128. /* ENDIF_PROC
  129.  *
  130.  * #endif conditional compilation directive.
  131.  */
  132. endif_proc()
  133. {
  134. int    i ;
  135.  
  136. /* on missing #ifdef statement, abort */
  137. if ( ifdef_count <= 0 ) {
  138.     sprintf( errline, "#Endif: no matching ifdef: %s", in_buff ) ;
  139.     abort( errline ) ;
  140. }
  141.  
  142. ifdef_count-- ;
  143.  
  144. /* set a flag to inhibit input if any ifdef flags are FALSE */
  145. ignore_flag = FALSE ;
  146. for ( i=0; i<ifdef_count; i++ )
  147.     if ( ifdef_list[i] == FALSE ) ignore_flag = TRUE ;
  148.  
  149. /* signal that in_buff is empty */
  150. IN_BUFF_DONE
  151. }
  152.  
  153.  
  154.  
  155. /* INCLUDE_PROC
  156.  *
  157.  * Handle file inclusion
  158.  *
  159.  * P. R. OVE  11/9/85
  160.  */
  161.  
  162. include_proc()     
  163. {                  
  164. char    *pntr, *open_parens, *close_parens, *name ;
  165.  
  166. /* This routine could be called when the conditional compilation
  167.  * flag has been set (#include is in the same group).
  168.  */
  169. if ( ignore_flag ) { IN_BUFF_DONE ; return ; }
  170.  
  171. /* get the file name */
  172. open_parens = line_end( first_nonblank + name_length ) ;
  173. if ( NULL == ( close_parens = mat_del( open_parens ) ) ) {
  174.     sprintf( errline, "INCLUDE: syntax: %s", in_buff ) ;
  175.     abort( errline ) ;
  176. }
  177. name = open_parens+1 ;
  178. *close_parens = NULL ;
  179.  
  180. /* push the old input file handle onto the filestack */
  181. if ( NULL == pushfile(&in) ) {
  182.     sprintf( errline, "INCLUDE: nesting too deep: %s", in_buff ) ;
  183.     abort( errline ) ;
  184. }
  185.  
  186. /* open the new file */
  187. if ( NULL == ( in = fopen( name, "r" ) ) ) {
  188.     sprintf( errline, "INCLUDE: can't open file: %s", name ) ;
  189.     abort( errline ) ;
  190. }
  191.  
  192. IN_BUFF_DONE ;
  193. }
  194.  
  195.  
  196. /* push a file handle onto the filestack.  return NULL on error. */
  197. int    pushfile(handleaddress)
  198. FILE    *(*handleaddress) ;
  199. {
  200.     if ( include_count >= NESTING ) return(NULL) ;
  201.     filestack[include_count] = *handleaddress ;
  202.     include_count++ ;
  203.     return(1) ;
  204. }
  205.  
  206.  
  207. /* pop a file handle from the filestack.  return NULL on error */
  208. int    popfile(handleaddress)
  209. FILE    *(*handleaddress) ;
  210. {
  211.     if ( include_count <= 0 ) return(NULL) ;
  212.     include_count-- ;
  213.     *handleaddress = filestack[include_count] ;
  214.     return(1) ;
  215. }
  216.  
  217.